#ifndef __7BC2D1ED_03EB_483f_B89C_1C0063E0C7AE__
#define __7BC2D1ED_03EB_483f_B89C_1C0063E0C7AE__

#include "../import/head.h"
#include "../tools/head.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos
{
    // 
    // select_impl
    // 
    class select_impl : public xos_stl::mem_pool_item< select_impl, thread_lock >,
        public i_select
    {
    public:
        enum
        {
            MAX_SELECT_NUM = 1000
        };

    public:
        select_impl();
        ~select_impl();

    public:
        int init();
        int term();

    protected:
        // 
        // internal parameters
        // 
        i_handle * m_pWrites[MAX_SELECT_NUM];
        SOCKET m_sWrites[MAX_SELECT_NUM];
        int m_nWritesNum;

        i_handle * m_pErrors[MAX_SELECT_NUM];
        SOCKET m_sErrors[MAX_SELECT_NUM];
        int m_nErrorsNum;

        i_handle * m_pReads[MAX_SELECT_NUM];
        SOCKET m_sReads[MAX_SELECT_NUM];
        int m_nReadsNum;

    protected:
        int prepare_select_handles( i_handle * pInputHandles[], int nInputNum, 
            i_handle ** pOutputHandles, SOCKET * pOutInts, int & nOutNum );

        int helper_select( i_handle * pRead[], int & nReadNum, 
            i_handle * pWrite[], int & nWriteNum, 
            i_handle * pError[], int & nErrorNum, 
            xos::xos_u32 uTimeout );

    public:
        // 
        // select function. the same as os select.
        // 
        // -1    : error
        // 0     : timeout
        // other : select num
        // 
        int select( i_handle * pRead[], int & nReadNum, 
            i_handle * pWrite[], int & nWriteNum, 
            i_handle * pError[], int & nErrorNum, 
            xos::xos_u32 uTimeout );

        // 
        // release
        // 
        int release();
    };
} // xos

#endif // __7BC2D1ED_03EB_483f_B89C_1C0063E0C7AE__
